//
//  SoundViewController.m
//  安好
//
//  Created by 冯凯 on 14-11-21.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SoundViewController.h"

@interface SoundViewController ()

@end

@implementation SoundViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    //加载导航条
    [self makeNavBar];
    //加载数据
    [self loadData];
    //加载tableView
    [self createTableView];
    
}
/**
 *  创建数据
 */
-(void)loadData
{
    self.soundArray = @[@"系统一",@"系统二",@"系统三"];
}
#pragma mark - 配置导航条
-(void)makeNavBar
{
    self.navigationItem.title = @"铃声设置";
    // 返回按钮图片
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backMoreVC)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}
#pragma mark - 创建tableView设置代理
-(void)createTableView
{
    self.systemTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth,kScreenHeight - iOS7TopMargin) style:UITableViewStyleGrouped];
    self.systemTableView.delegate = self;
    self.systemTableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.systemTableView.showsHorizontalScrollIndicator = NO;
    self.systemTableView.showsVerticalScrollIndicator = NO;
    self.systemTableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    [self.view addSubview:self.systemTableView];
}
 - (void)backMoreVC
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.soundArray.count;
}

//  下面两个数据源方法是必须实现的
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"myCell";
    
    
    //  检测、查询是否有闲置的单元格
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell =  [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    if (kScreenWidth == 414){
        cell.textLabel.font = [UIFont systemFontOfSize:17];
    }else{
        
        cell.textLabel.font = [UIFont systemFontOfSize:15];
    }
    cell.textLabel.textColor = [UIColor colorWithRed:31.0/255 green:33.0/255 blue:36.0/255 alpha:1];
    cell.textLabel.text = self.soundArray[indexPath.row];
    //  返回每行的单元格对象
    return cell;
}
/**
 *  判断选择的哪个
 */
NSIndexPath * lastIndexPath = nil;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
        int newRow = [indexPath row];
        int oldRow = [lastIndexPath row];
        
        if (newRow != oldRow)
        {
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                        indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            
            UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:
                                        lastIndexPath];
            oldCell.accessoryType = UITableViewCellAccessoryNone;
            
            lastIndexPath = indexPath;
        }else{
            UITableViewCell *newCell = [tableView cellForRowAtIndexPath:
                                        indexPath];
            newCell.accessoryType = UITableViewCellAccessoryCheckmark;
            lastIndexPath = indexPath;

        }
    
    NSBundle * bundle = [NSBundle mainBundle];
    // 音频文件路径
    if (indexPath.row == 0) {
        self.playerUrl = [bundle pathForResource:@"系统一" ofType:@"mp3"];
    }else if (indexPath.row == 1){
        self.playerUrl = [bundle pathForResource:@"系统二" ofType:@"mp3"];
    }else{
        self.playerUrl = [bundle pathForResource:@"系统三" ofType:@"mp3"];
    }
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString * uid = [[UserManager shareInstance]getUserInfo].uid;
    NSString * soundKey = [NSString stringWithFormat:@"Sound%@",uid];
    [user setObject:self.soundArray[indexPath.row] forKey:soundKey];
    // 初始化本地的url
    NSURL * url = [[NSURL alloc]initFileURLWithPath:self.playerUrl];
    
    self.player = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
    self.player.delegate= self;
    // 分配播放所需的资源，并将其加入内部播放队列
    [self.player prepareToPlay];
    
    // 播放
    if ([self.player isPlaying]){
        [self.player stop];
    }else{
        [self.player play];

    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (kScreenWidth == 414){
        return 56;
        
    }else{
        
        return 46;
    }
}

//设置段头高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 10;
  
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
{
    // 播放结束时执行的动作
    [self.player stop];
    self.player.delegate = nil;
    self.player = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
